package wang.chunfan.springcloud.learning.nacos.ribbon.server.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import wang.chunfan.springcloud.learning.nacos.ribbon.server.model.Result;
import wang.chunfan.springcloud.learning.nacos.ribbon.server.model.User;


/**
 * @version 1.0
 * @Author wangchunfan
 * @Date 2020/5/22 11:58
 **/
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private RestTemplate restTemplate;

    @Value("${server-url.consul-user-server}")
    private String userServerUrl;

    @PostMapping
    public Result insert(@RequestBody User user) {
        return restTemplate.postForObject(userServerUrl + "/user", user, Result.class);
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Long id) {
        // 注意： delete 没有返回值
        restTemplate.delete(userServerUrl + "/user/{1}", id);
        return new Result();
    }

    @PutMapping
    public Result update(@RequestBody User user) {
        // 注意： put 没有返回值
        restTemplate.put(userServerUrl + "/user", user);
        return new Result();
    }

    @GetMapping("/{id}")
    public Result get(@PathVariable Long id) {
        return restTemplate.getForObject(userServerUrl + "/user/{1}", Result.class, id);
    }
}
